package org.iteam.xlz.core.service.base;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.iteam.commons.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.base.ColumnInfo;
import org.iteam.xlz.core.entity.base.ColumnType;

public class ColumnService extends BaseService<ColumnInfo>{
	private ColumnService(){}
	
	private static ColumnService instance;
	
	public static ColumnService getInstance(){
		if(null==instance){
			instance=new ColumnService();
		}
		return instance;
	}
	
	public long counts(){
		try {
			return super.counts("select count(1) from colum_info where 1=1");
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<ColumnInfo> query(long page,long rows){
		try {
			return super.query("select a.*,b.name typeName from colum_info a left join colum_type b on a.typeId = b.id  where 1=1 limit ?,?",(page-1)*rows,rows);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<ColumnInfo>();
		}
	}
	
	
	public ColumnInfo findOne(String id){
		try {
			return super.show("select a.*,b.name typeName from colum_info a left join colum_type b on a.typeId = b.id where 1=1 and a.id=?",id);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ColumnInfo();
		}
	}
	
	
	public boolean create(ColumnInfo model){
		SqlBuilder sql=SqlBuilder.createInsert("colum_info");
		sql.addField("id",UUID.randomUUID().toString().replace("-","").toUpperCase());
		sql.addField("typeId",model.getTypeId());
		sql.addField("title",model.getTitle());
		sql.addField("contentType",model.getContentType());
		sql.addField("content",model.getContent());
		sql.addField("isRecommand",model.getIsRecommand());
		sql.addField("subTitle",model.getSubTitle());
		sql.addField("status",model.getStatus());
		sql.addField("creatTime",DateUtil.currentTime());
		sql.addField("creator",model.getCreator());
		sql.addField("showOrder",model.getShowOrder());
		sql.addField("imgUrl",model.getImgUrl());
		sql.addField("imgType",model.getImgType());
		try {
			return super.updates(sql.getSql(),sql.getParams())==1;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean update(ColumnInfo model){
		SqlBuilder sql=SqlBuilder.createUpdate("colum_info");
		sql.addField("typeId",model.getTypeId());
		sql.addField("title",model.getTitle());
		sql.addField("contentType",model.getContentType());
		sql.addField("content",model.getContent());
		sql.addField("isRecommand",model.getIsRecommand());
		sql.addField("subTitle",model.getSubTitle());
		sql.addField("status",model.getStatus());
		sql.addField("showOrder",model.getShowOrder());
		sql.addField("imgUrl",model.getImgUrl());
		sql.addField("imgType",model.getImgType());
		sql.addWhere("id",model.getId());
		try {
			return super.updates(sql.getSql(),sql.getParams())==1;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean delete(String id){
		try {
			return super.updates("delete from colum_info where id=?",id)>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
}
